package com.xijin.milkytea.ui.widget

import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.util.Log
import android.view.MotionEvent
import android.view.View
import com.xijin.milkytea.R

class MaterialProgressBar : View {
    private lateinit var allBgPaint: Paint
    private lateinit var allBgPaintException: Paint
    private lateinit var progressPaint: Paint
    private lateinit var progressPaintException: Paint
    private lateinit var mContext: Context

    private var mWidth = 0
    private var mHeight = 0
    private var preStartColor = 0
    private var preEndColor = 0

    //蓝色进度条距背景边距
    private var padding = 0f

    //范围 首端按钮圆心-末端按钮圆心
    private var moveX = 0f
    private var moveY = 0f
    //延迟重新加载数值
    var percent=0f
    //是否可用
    var isCanScroll = true

    //是否异常状态
    private var isException = false

    constructor(context: Context) : this(context, null)

    constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, 0)

    constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : super(
        context, attrs, defStyleAttr
    ) {
        init(context)
    }

    private fun init(context: Context) {
        this.mContext = context
        padding = dp2px(mContext, 3f).toFloat()

        allBgPaint = Paint()
        allBgPaint.setAntiAlias(true)
        allBgPaint.setStyle(Paint.Style.STROKE)
        allBgPaint.strokeWidth = dp2px(mContext, 1f).toFloat()
        allBgPaint.setColor(mContext.getResources().getColor(R.color.color_45b035))

        allBgPaintException = Paint()
        allBgPaintException.setAntiAlias(true)
        allBgPaintException.setStyle(Paint.Style.STROKE)
        allBgPaintException.strokeWidth = dp2px(mContext, 1f).toFloat()
        allBgPaintException.setColor(mContext.getResources().getColor(R.color.color_999999))

        progressPaint = Paint()
        progressPaint.isAntiAlias = true
        progressPaint.style = Paint.Style.FILL
        progressPaint.color = Color.parseColor("#FFFFFF")
        preStartColor = mContext.getResources().getColor(R.color.blue_start)
        preEndColor = mContext.getResources().getColor(R.color.blue_end)
        progressPaint.shader =
            LinearGradient(padding, mHeight / 2f, mWidth - padding, mHeight / 2f, preStartColor, preEndColor, Shader.TileMode.CLAMP)

        progressPaintException = Paint()
        progressPaintException.isAntiAlias = true
        progressPaintException.style = Paint.Style.FILL
        progressPaintException.color = Color.parseColor("#FFFFFF")
        preStartColor = mContext.getResources().getColor(R.color.color_999999)
        preEndColor = mContext.getResources().getColor(R.color.color_999999)
        progressPaintException.shader =
            LinearGradient(padding, mHeight / 2f, mWidth - padding, mHeight / 2f, preStartColor, preEndColor, Shader.TileMode.CLAMP)

    }

    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
//        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        val defaltWidth = getDefaultSize(suggestedMinimumWidth, widthMeasureSpec)
        val defaltHeight = getDefaultSize(suggestedMinimumHeight, heightMeasureSpec)
        mWidth =32
        mHeight =defaltHeight
        setMeasuredDimension(dp2px(mContext, 32f), defaltHeight)
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        mWidth = width
        mHeight = height
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        canvas?.apply {
            //整体背景
            canvas.drawRoundRect(RectF(0f, 0f, mWidth.toFloat(), mHeight.toFloat()), 4f, 4f, if (isException) allBgPaintException else allBgPaint)


            moveY = (mHeight - padding * 2) * (1-percent)+padding
            //进度
            canvas.drawRoundRect(
                RectF(padding, moveY /*+ ic_temperature_circle.getWidth() / 2f+ padding*/, mWidth - padding, mHeight - padding),
                2f,
                2f,
                if (isException) progressPaintException else progressPaint
            )
        }
    }

    override fun onTouchEvent(event: MotionEvent?): Boolean {
//        return super.onTouchEvent(event)

        when (event?.action) {
            MotionEvent.ACTION_DOWN ->
                //Log.e("gem", "onTouchEvent: ACTION_DOWN" );
                if (!isCanScroll) {
                    return false
                }
            MotionEvent.ACTION_MOVE -> {
                Log.e("gem", "onTouchEvent: ACTION_MOVE" + event.y);
                moveX = event.x
                moveY = event.y
                //限制progress边界  范围 首端按钮圆心-末端按钮圆心
                // (mHeight/2f-ic_temperature_circle.getHeight()/2f)
                if (moveY > padding && moveY < mHeight - padding) {
                    val value: Float = (moveY - padding) / (mHeight - padding * 2)
                    Log.e("gem", "onTouchEvent: $value")

                    val intVal = (value + 0.5f).toInt()
                    onProgressChangeListener?.apply {
                        onProgress(intVal + 0f)
                    }

                } else {
                    if (moveY < padding) {
                        moveY = padding
                    }
                    if (moveY > mHeight - padding) {
                        moveY = mHeight - padding
                    }
                }
                invalidate()
            }
            MotionEvent.ACTION_UP -> {
                //                Log.e("gem", "onTouchEvent: ACTION_UP" + event.getX());
                val clickX = event.x
                val clickY = event.y
            }
        }
        return true
    }

    //percent 0-1f  moveY 从上到下递增，表示空白的高度
    //0 绿条0 1绿条满
    fun setProgress(percent: Float) {
        this.percent=percent
        moveY = (mHeight - padding * 2) * (1-percent)+padding
        invalidate()
    }

    fun getProgress(): Float {
        val value: Float = (moveY - padding) / (mHeight - padding * 2)
        return value
    }

    fun dp2px(context: Context, dpValue: Float): Int {
        val scale = context.resources.displayMetrics.density
        return (dpValue * scale + 0.5f).toInt()
    }

    interface OnProgressChangeListener {
        fun onProgress(value: Float)
    }

    var onProgressChangeListener: OnProgressChangeListener? = null

    fun setProgressException(b: Boolean) {
        isException = b
        invalidate()
    }

}